package com.yutel.system.struts.core;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.yutel.system.repository.SysUser;

/**
 * BaseAction.java Create on Nov 18, 20088:31:36 PM
 * 
 * 
 * 基本Action
 * Copyright 2003-2008 XinLi.com.cn
 * @author CHC
 * @version 1.0
 */
@SuppressWarnings("serial")
public class BaseAction extends ActionSupport {
	protected Logger log = Logger.getLogger(this.getClass());

	public String jsonString;

	protected boolean isNotEmpty(String str){
		if(str != null && str.trim().length() != 0){
			if("请选择".equals(str.trim())) {
				return false;
			} else {
				return true;
			}
		}
		return false;
	}
	
	protected void preparePage(Page page){
		String s = getRequest().getParameter("start");
		String l = getRequest().getParameter("limit");
		
		int start = isNotEmpty(s) ? Integer.parseInt(s) : 0;
		int limit = isNotEmpty(l) ? Integer.parseInt(l) : 20;
		page.setStart(++start);
		page.setLimit(limit = limit == 0 ? 20 : limit);
		page.setEnd(page.getStart() + page.getLimit() - 1);
	}
	
	protected SysUser getSessionUser(){
		SysUser user = (SysUser) getSession().getAttribute("user");
		return user;
	}
	
	public void writeJsonStringByObject(Object obj){
		JSONArray jsonArray = JSONArray.fromObject(obj);
		jsonString = jsonArray.toString();
	}
	
	public void outJsonString(String str) {
		getResponse().setContentType("text/javascript;charset=UTF-8");
		outString(str);
	}

	/*
	 * public void outJson(Object obj) {
	 * outJsonString(JSONObject.fromObject(obj).toString()); }
	 * 
	 * public void outJsonArray(Object array) {
	 * outJsonArray(JSONArray.fromObject(array).toString()); }
	 */

	public void outString(String str) {
		try {
			PrintWriter out = getResponse().getWriter();
			out.write(str);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void outXMLString(String xmlStr) {
		getResponse().setContentType("application/xml;charset=UTF-8");
		outString(xmlStr);
	}

	/**
	 * 获得request
	 * 
	 * @return
	 */
	public static HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	/**
	 * 获得response
	 * 
	 * @return
	 */
	public HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	/**
	 * 获得session
	 * 
	 * @return
	 */
	public HttpSession getSession() {
		return getRequest().getSession();
	}

	/**
	 * 说明:获得servlet上下文
	 * 
	 * <pre>
	 * Example:
	 * </pre>
	 *
	 * @return ServletContext
	 * @authorCHC
	 */
	public ServletContext getServletContext() {
		return ServletActionContext.getServletContext();
	}
	public String getRealyPath(String path) {
		return getServletContext().getRealPath(path);
	}
	
	public static String getParameterValue(String str) {
	    String result = getRequest().getParameter(str);
//	    if (result == null || result.trim().length() == 0) {
//		result = "";
//	    } 
	    return result;
	}
}
